<template>
	<view class="page">
		<image :src="company.logo"></image>
		<view style="font-size: 30rpx;">{{company.name}}</view>
		<view class="radio">
		<!-- 	<u-checkbox-group style="flex: 0;" v-model="radio"> <u-checkbox ref="checkbox" shape="circle"
					name="1"></u-checkbox></u-checkbox-group>
			<view>
				我已经阅读同意《<text>浏览用户协议</text>》刊《<text>隐私协议</text>》
			</view> -->
		</view>
		<view style="position: relative;width: 100%;">
			<u-button v-if="!StorageOpenid" :disabled='disabled' open-type="getPhoneNumber"
				@getphonenumber="onGetPhoneNumber" iconColor="#fff" text="一键登录" color=" #3975c6"></u-button>
			<u-button v-else :disabled='disabled' @click='getCode' iconColor="#fff" text="一键登录"
				color=" #3975c6"></u-button>
			<!-- <view v-if="!radio.length" class="noRadio" @click="noRadio"></view> -->
		</view>
		<view style="position: relative;width: 100%;">
			<u-button :disabled='disabled' @click='jump' iconColor="#fff" text="密码登录" color=" #3975c6"></u-button>
		</view>
		<u-button :disabled='disabled' text="暂不登录" @click='back'></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				radio: [],
				disabled: false,
				loginCode: null,
				openid: null,
				unionid: null,
				Phone: null
			};
		},
		computed: {
			company() {
				return {
					name: uni.getStorageSync('companyName'),
					logo: uni.getStorageSync('companyLogo'),
				}
			},
			StorageOpenid() {
				return uni.getStorageSync('openid')
			}
		},
		methods: {
			jump() {
				this.$utils.navigateTo({
					url: '/login/password'
				})
			},
			getCode() {
				this.disabled = true
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						this.loginCode = loginRes.code
						this.getOpenidAndSession()
					},
					fail: (err) => {
						this.disabled = false
						uni.hideLoading()
					}
				});
			},
			loginSilent() {
				wx.getUserInfo({
					success: resUser => {
						this.$api.Login.Silent({
							openId: uni.getStorageSync('openid'),
							rawData: resUser.rawData,
							signature: resUser.signature,
							encryptedData: resUser.encryptedData,
							iv: resUser.iv
						}).then(res => {
							uni.setStorageSync('token', res.data.token);
							uni.setStorageSync('user', res.data.user);
							this.disabled = false
							uni.reLaunch({
								url: '/home_c/index'
							})
							uni.hideLoading()
						}).catch(err => {
							this.disabled = false
							uni.hideLoading()
						})
					}
				})
			},
			onGetPhoneNumber(e) {
				console.log(e)
				if (!e.detail.code) return
				this.disabled = true
				uni.showLoading({
					title: '登录中...',
				})
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						console.log(loginRes, 'success')
						this.loginCode = loginRes.code
						this.Phone = e.detail
						this.getOpenidAndSession()
					},
					fail: (err) => {
						console.log(err, 'fail')
						this.disabled = false
						uni.hideLoading()
					}
				});
			},

			getOpenidAndSession() {
				this.$api.Login.Session({
					code: this.loginCode
				}).then(res => {
					if (this.StorageOpenid) {

						this.loginSilent()
						return
					}
					this.openid = res.data.openid
					this.unionid = res.data.unionid
					this.login()
				}).catch(err => {
					this.disabled = false
					uni.hideLoading()
				})
			},
			login() {
				wx.getUserInfo({
					success: resUser => {
						this.$api.Login.Mobile({
							openid: this.openid,
							unionid: this.unionid,
							code: this.Phone.code,
							iv: this.Phone.iv,
							encryptedData: this.Phone.encryptedData,
							rawData: resUser.rawData,
							signature: resUser.signature,
							userInfo: {
								...resUser.userInfo
							},
						}).then(res => {
							uni.setStorageSync('openid', this.openid);
							uni.setStorageSync('token', res.data.token);
							uni.setStorageSync('user', res.data.user);
							uni.reLaunch({
								url: uni.getStorageSync('pageUrl')
							})
							uni.hideLoading()
						}).catch(err => {
							this.disabled = false
							uni.hideLoading()
						})
					}
				})
			},
			back() {
				this.$utils.back()
			},
			noRadio() {
				this.$u.toast('请先勾选');
				this.radio=['1']
			}
		}
	}
</script>

<style lang="scss">
	.page {
		height: 60vh;
		color: #010101;
		font-size: 26rpx;
		width: calc(100% - 160rpx);
		margin: auto;
		display: flex;
		flex-direction: column;
		align-items: center;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		justify-content: space-between;
	}

	image {
		width: 150rpx;
		height: 150rpx;
	}



	.radio {
		text {
			color: #0000ED;
		}

		flex: 0.8;
		display: flex;
		justify-content: flex-start;
		align-items: flex-end;
	}

	.noRadio {
		position: absolute;
		width: 100%;
		height: 100%;
		background-color: #010;
		z-index: 11;
		top: 0;
		opacity: 0;
	}
</style>